package us.codecraft.webmagic.samples.tuchong;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.dao.TcAuthorDao;
import us.codecraft.webmagic.dao.TcPicDao;
import us.codecraft.webmagic.model.samples.tochong.Author;
import us.codecraft.webmagic.model.samples.tochong.Pic;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;

/**
 * @Author : xuaoping
 * @Date: 2021/7/28
 */
@Component
@Slf4j
public class TcPipeline implements Pipeline {
    @Autowired
    private TcPicDao tcPicDao;
    @Autowired
    private TcAuthorDao tcAuthorDao;

    @Override
    public void process(ResultItems resultItems, Task task) {
        try {
            List<Pic> pics = resultItems.get("picList");
            if (pics != null && !pics.isEmpty()) {
                tcPicDao.batchInsert(pics);
            }
            List<Author> authors = resultItems.get("authors");
            if (authors != null && !authors.isEmpty()) {
                tcAuthorDao.batchInsert(authors);
            }
            Pic updatePic = resultItems.get("updateExif");
            if (updatePic != null) {
                tcPicDao.updateByImageId(updatePic);
            }
        } catch (Exception e) {
            log.info("TcPipeline preocess error:{}", resultItems, e);
        }

    }
}
